package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * Selector2类
 * 实现选择器2的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class Selector2 implements AxdrType {

    private byte[] dataCode = null;
    private OAD oad = null;
    private Data sval = null;
    private Data eval = null;
    private Data inter = null;

    /**
     * 默认构造函数
     */
    public Selector2() {
    }

    /**
     * 设置数据编码
     * 
     * @param dataCode 数据编码
     */
    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    /**
     * 设置所有参数
     * 
     * @param oad   对象属性描述符
     * @param sval  起始值
     * @param eval  结束值
     * @param inter 间隔
     */
    public void setAll(OAD oad, Data sval, Data eval, Data inter) {
        this.oad = oad;
        this.sval = sval;
        this.eval = eval;
        this.inter = inter;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        oad = new OAD();
        codeLength += oad.decode(input);

        sval = new Data();
        codeLength += sval.decode(input);

        eval = new Data();
        codeLength += eval.decode(input);

        inter = new Data();
        codeLength += inter.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (dataCode != null) {
            codeLength = dataCode.length;
            for (int i = dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException("IO exception during encode", e);
                }
            }
        } else {
            if (oad != null && sval != null && eval != null && inter != null) {
                codeLength = 0;
                codeLength += inter.encode(output);
                codeLength += eval.encode(output);
                codeLength += sval.encode(output);
                codeLength += oad.encode(output);
            } else {
                throw new RuntimeException("oad or sval or eval or inter is null");
            }
        }
        return codeLength;
    }

    /**
     * 编码并保存
     * 
     * @param encodingSizeGuess 编码大小预估
     */
    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    /**
     * 获取对象属性描述符
     * 
     * @return 对象属性描述符
     */
    public OAD getOad() {
        return oad;
    }

    /**
     * 获取起始值
     * 
     * @return 起始值
     */
    public Data getSval() {
        return sval;
    }

    /**
     * 获取结束值
     * 
     * @return 结束值
     */
    public Data getEval() {
        return eval;
    }

    /**
     * 获取间隔
     * 
     * @return 间隔
     */
    public Data getInter() {
        return inter;
    }

    @Override
    public String toString() {
        if (oad != null && sval != null && eval != null && inter != null) {
            return "sequence: {" + "oad: " + oad + ", sval: " + sval + ", eval: " + eval + ", inter: " + inter + "}";
        } else {
            return "oad or sval or eval or inter is null";
        }
    }
}